<template>
  <div>

        <!-- title -->
        <PageTitle>
          {{ $t('menu.M_1040000406') }}
    </PageTitle>
    <el-card
      shadow="never"
      :body-style="{ padding: '0px' }"
      :page-size="pageSize"
      :total="totalPage"
      :current-page="pageIndex"
      @current-change="currentChangeHandle"
    >
      <el-form class="p-4" inline >
        <el-form-item>
          <el-input
            maxlength="64"
            v-model="devSn"
            @keyup.enter.native="fetchData((pageIndex = 1))"
            @blur="() => (devSn = devSn.trim())"
            :placeholder="$t('sysapp.devSn')"
            clearable
            @clear="fetchData((pageIndex = 1))"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="appPackageName"
            clearable
            filterable
            :placeholder="$t('sysapp.select_app')"
            @change="getSystemAppVersion"
          >
            <el-option
              v-for="(item, index) in appList"
              :key="item.appFileId"
              :label="item.appName"
              :value="item.appPackageName"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="version"
            filterable
            clearable
            @change="fetchData((pageIndex = 1))"
            :placeholder="$t('sysapp.select_version')"
          >
            <el-option
              v-for="(item, index) in versionList"
              :key="item.appFileId"
              :label="item.versionName"
              :value="item.versionName"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="w-8"
            @click="fetchData((pageIndex = 1))"
          >
            <svg-icon icon-class="Search"/>
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-tooltip
            class="item"
            effect="dark"
            :content="$t('sysapp.export_device_info')"
            placement="top"
          >
            <el-button
              v-show="isAuth('1040000406.QUERY') && totalPage != 0"
              type="primary"
              class="w-8"
              :loading="btnLoading"
              @click="exportDeviceAppInfo"
            >
              <svg-icon icon-class="download"/>
            </el-button>
          </el-tooltip>
        </el-form-item>
      </el-form>

      <el-table
        :show-overflow-tooltip="true"
        v-loading="listLoading"
        :data="tableData"
        stripe
      >
        <el-table-column
          :label="$t('sysapp.devSn')"
          prop="devSn"
        />

        <el-table-column
          :label="$t('appstore.application.appName')"
          prop="pkgName"
        >
          <template v-slot="{ row }">
            <div
              class="cursor-pointer underline"
              @click="
                () =>
                  $router.push({
                    path: '/tms/resources/app-detail',
                    query: { packageName: row.pkgName, appName: row.appName }
                  })
              "
            >
              {{ row.appName }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('sysapp.version')"
          prop="currentVersion"
          
        />
        <el-table-column
          :label="$t('common.createTime')"
          prop="creTime"
          
        >
          <template v-slot="{ row }">
            <div :title="timeFilter(row.creTime)">
              {{ dateFilter(row.creTime) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('common.install_time')"
          prop="updTime"
          
        >
          <template v-slot="{ row }">
            <div :title="timeFilter(row.updTime)">
              {{ dateFilter(row.updTime) }}
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { timeFilter, dateFilter } from '@/filters'
import { isAuth } from '@/utils'
import {
  getSystemApp,
  getSystemAppVersion,
  getDeviceAppInfo,
  exportDeviceAppInfo
} from '@/api/app'
import PageTitle from '@/components/PageTitle/index.vue'
export default {
  components:{
    PageTitle
  },
  data() {
    return {
      appList: [],
      appPackageName: '',
      versionList: [],
      version: '',
      devSn: '',
      btnLoading: false,
      tableData: [],
      listLoading: false,
      selectedData: [],
      paginationShow: true,
      condition: '',
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      addRoleVisible: false,
      operationType: '',
      editRoleId: ''
    }
  },
  mounted() {
    this.getSystemApp()
    this.fetchData()
  },
  methods: {
    isAuth,
    timeFilter,
    dateFilter,
    fetchData() {
      this.listLoading = true
      const params = {}
      if (this.appPackageName !== '') {
        params.appPackageName = this.appPackageName
      }
      if (this.version !== '') {
        params.appVersion = this.version
      }
      if (this.devSn !== '') {
        params.devSn = this.devSn
      }
      params.charactorCode = 'ADMIN'
      params.pageNo = this.pageIndex
      params.pageSize = this.pageSize

      getDeviceAppInfo(params)
        // getDeviceAppInfo()
        .then((res) => {
          this.listLoading = false
          this.tableData = res.data
          this.totalPage = res.rows
        })
        .catch((e) => {
          this.listLoading = false
          this.$confirmError(e)
        })
    },
    exportDeviceAppInfo() {
      this.btnLoading = true
      const params = {}
      if (this.appPackageName !== '') {
        params.appPackageName = this.appPackageName
      }
      if (this.version !== '') {
        params.appVersion = this.version
      }
      if (this.devSn !== '') {
        params.devSn = this.devSn
      }
      params.charactorCode = 'ADMIN'
      this.$alert(this.$t('common.export_tip'), this.$t('common.tips'), {
        showConfirmButton: false
      })
      exportDeviceAppInfo(params)
        .then((res) => {
          this.btnLoading = false
          this.$store.dispatch('app/fetchTaskData')
          setTimeout(() => {
            this.$msgbox.close()
          }, 3000)
        })
        .catch((e) => {
          this.btnLoading = false
          this.$confirmError(e)
        })
    },
    getSystemApp() {
      this.appList = []
      getSystemApp()
        .then((res) => {
          this.appList = res
        })
        .catch(this.$confirmError)
    },
    getSystemAppVersion() {
      this.versionList = []
      this.version = []
      if (this.appPackageName === '') {
        this.fetchData((this.pageIndex = 1))
        return
      }
      const param = {
        appPackageName: this.appPackageName
      }
      getSystemAppVersion(param)
        .then((res) => {
          this.versionList = res
        })
        .catch(this.$confirmError)
      this.fetchData((this.pageIndex = 1))
    },

    currentChangeHandle(val) {
      this.pageIndex = val
      this.fetchData()
    }
  }
}
</script>

<style lang="scss">
.terminal-list {
  .el-table .cell {
    /* height: 30px !important; */
  }
}
</style>

<style lang="scss" scoped>
.query-button {
  margin-left: 30px;
}
.svg-container {
  cursor: pointer;
  margin-top: 6px;
  margin-left: 20px;
  font-size: 24px;
  display: inline-block;
}

.pagination-wrapper {
  text-align: right;
}
</style>
